package com.bigfish.gbs.controller;

import com.alibaba.fastjson.JSON;
import com.bigfish.gbs.dto.GoodsVO;
import com.bigfish.gbs.dto.Result;
import com.bigfish.gbs.dto.SupplierVO;
import com.bigfish.gbs.entity.Category;
import com.bigfish.gbs.entity.City;
import com.bigfish.gbs.entity.Supplier;
import com.bigfish.gbs.enums.ResultEnum;
import com.bigfish.gbs.service.CategoryService;
import com.bigfish.gbs.service.CityService;
import com.bigfish.gbs.service.GoodsService;
import com.bigfish.gbs.service.SupplierService;
import com.bigfish.gbs.util.FileUploadUtil;
import com.bigfish.gbs.util.ResultUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;


/**
 * @Author Joycessh
 * @Description
 * @Date 12/20/18 9:36 AM
 */
@Api(value = "商品管理", description = "与商品管理相关的接口")
@RestController
@RequestMapping("/goods")
public class GoodsCtroller extends BasicAction {

    private static final Logger LOGGER = LoggerFactory.getLogger(GoodsCtroller.class);

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private CityService cityService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private GoodsService goodsService;

    @ApiOperation(value = "供应商选择", httpMethod = "GET")
    @GetMapping("/supplier")
    public Result getSupplier() {
        List<Supplier> list = supplierService.getSupplier();
        return ResultUtil.retFormat(ResultEnum.SUCCESS).addData("supplier", list);
    }

    @ApiOperation(value = "获取城市", httpMethod = "GET")
    @GetMapping("/city")
    public Result getCity() {
        List<City> list = cityService.getCity();
        return ResultUtil.retFormat(ResultEnum.SUCCESS).addData("city", list);
    }

    @ApiOperation(value = "获取商品类别", httpMethod = "GET")
    @GetMapping("/category")
    public Result getCategory() {
        List<Category> list = categoryService.getCategory();
        return ResultUtil.retFormat(ResultEnum.SUCCESS).addData("category", list);
    }

    @RequestMapping("/image")
    public Result imageUpload(HttpServletRequest req, MultipartHttpServletRequest multiReq) throws IOException {
        MultipartFile file = multiReq.getFile("file");
        String path = FileUploadUtil.fileUpload(file, req);
        return ResultUtil.retFormat(ResultEnum.SUCCESS).addData("imgPath", path);
    }

    @ApiOperation(value = "添加商品", httpMethod = "POST")
    @PostMapping("")
    public Result addGoods(@RequestBody HashMap map) {
        GoodsVO goodsVO = JSON.parseObject(JSON.toJSONString(map.get("params")), GoodsVO.class);
        boolean flag = goodsService.addGoods(goodsVO);
        if (flag) {
            return ResultUtil.retFormat(ResultEnum.SUCCESS);
        }else {
            return ResultUtil.retFormat(ResultEnum.FAIL);
        }
    }

    @ApiOperation(value = "修改商品", httpMethod = "PUT")
    @PutMapping("")
    public Result updateGoods(@RequestBody HashMap map) {
        GoodsVO goodsVO = JSON.parseObject(JSON.toJSONString(map.get("params")), GoodsVO.class);
        boolean flag = goodsService.updateGoods(goodsVO);
        if (flag) {
            return ResultUtil.retFormat(ResultEnum.SUCCESS);
        }else {
            return ResultUtil.retFormat(ResultEnum.FAIL);
        }
    }

    @ApiOperation(value = "删除商品", httpMethod = "DELETE")
    @DeleteMapping("")
    public Result removeGoods(@RequestParam Integer goodsId) {
        boolean flag = goodsService.removeGoods(goodsId);
        if (flag) {
            return ResultUtil.retFormat(ResultEnum.SUCCESS);
        }else {
            return ResultUtil.retFormat(ResultEnum.FAIL);
        }
    }

    @ApiOperation(value = "通过项目名称查询商品", httpMethod = "GET")
    @GetMapping("")
    public Result getGoodsByName(@RequestParam String name,
                                 @RequestParam Integer currentPage,
                                 @RequestParam Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<GoodsVO> list = goodsService.getGoodsByName(name);
        PageInfo pageInfo =new PageInfo(list);
        return ResultUtil.retFormat(ResultEnum.SUCCESS).addData("goods", pageInfo);
    }
}
